package cn.jinjimi.app.entity.AppConfig;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Roy
 * Date: 16/5/12
 * @author zhenhua
 */
public class AppModule {
    public static final String ModuleAll = "all";
    public static final String ModuleSettings = "settings";
    public static final String ModuleToolbars = "toolbars";
    public static final String ModuleHome = "home";
    public static final String ModuleTabs = "tabs";
    public static final String ModuleDomains = "domains";
    public static final String ModuleDrp = "drp";
    public static final String ModuleAdv = "advertisements";
    public static final String ModulePoster = "posters";
    public static final String ModuleAlert = "alert";
    public static final String ModuleCRM = "customers";
    public static final String ModuleStudyIdea = "insurance_idea";
    public static final String ModuleResource = "resource";
    public static final String ModuleBundle = "bundles";
    public static final String ModuleTheme = "theme";

    public String name;
    public String link;
    public String config;

    public static AppModule insertByJsonObject(JSONObject jsonObject) {
        AppModule appModule = new AppModule();

        if (jsonObject.has("name"))
            appModule.name = jsonObject.optString("name");

        if (jsonObject.has("link"))
            appModule.link = jsonObject.optString("link");

        if (jsonObject.has("config")) {
            appModule.config = jsonObject.optString("config");
            if (appModule.config.equals("null"))
                appModule.config = "";
        }

        return appModule;
    }

    public static AppModule insertByStr(String json) {
        try {
            JSONObject jsonObject = new JSONObject(json);
            if (jsonObject.has("data"))
                return AppModule.insertByJsonObject(jsonObject.optJSONObject("data"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }
}
